/*
 *  
 * */
package com.example.gametestproject;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;

import com.xxxx.game.*;

public abstract class SpriteBasic implements ISprite
{
	public PointF mObjPos; // current position
	protected Paint paint;//control paint effect, for example setAlpha
	private int mResourceIndex; // current frame index
	protected int mZOrder = 0;
	private IAnimation mAnimationData;
	private boolean mIsSelected = false;
	
	public SpriteBasic()
	{
		this.mObjPos = new PointF(0, 0);
		this.paint = new Paint();
		this.mAnimationData = null;
		this.mResourceIndex = -1;
		//this.mFPS = 50;
	}
	
	public SpriteBasic(int resourceIdx, PointF point)
	{
		this.paint = new Paint();
		this.mResourceIndex = resourceIdx;
		this.mObjPos = point;
		this.mAnimationData = null;
	}
	
	public SpriteBasic(int resourceIdx, PointF point, IAnimation animationData)
	{
		this.paint = new Paint();
		this.mObjPos = point;
		this.mResourceIndex = resourceIdx;
		this.mAnimationData = animationData;
	}

	@Override
	//update sprite state, position/alpha etc
	public void update()
	{
		if (this.mAnimationData != null && !mAnimationData.update(this))
		{
			mAnimationData = this.mAnimationData.getNext();
		}
	}
	
	public void setPosition(PointF point)
	{
		this.mObjPos = point;
	}

	public PointF getPosition()
	{
		return this.mObjPos;
	}
	public PointF getCenter()
	{
		return new PointF(this.mObjPos.x + this.getSize().Width / 2, this.mObjPos.y + this.getSize().Height / 2);
		
	}
	
	@Override
	public boolean isLiving()
	{
		return true;
	}
	
	@Override
	public int getZOrder()
	{
		return mZOrder;
	}
	
	@Override
	public void setZOrder(int zorder)
	{
		mZOrder = zorder;
	}
	
	@Override
	public void setSpriteAnimation(IAnimation animat)
	{
		this.mAnimationData = animat;
	}

	public IAnimation getSpriteAnimation()
	{
		return this.mAnimationData;
	}
	
	public boolean isMoving()
	{
		return this.mAnimationData != null && this.mAnimationData.isMoving();
	}
	
	//public boolean isAnimating()
	//{
	//	return this.mAnimationData != null && !this.mAnimationData.isStoped();
	//}
	
	public RectF getRect()
	{
		SizeF size = getSize();
		RectF r = new RectF(this.mObjPos.x, this.mObjPos.y, 
				this.mObjPos.x + size.Width, this.mObjPos.y + size.Height);
		return r;
	}

	public SizeF getSize()
	{
		SizeF size = new SizeF();
		
		Bitmap bmp = ResourceManager.getInstance().getBitmap(this.getCurrentFrame());
		if (bmp != null)
		{
			size.Width = bmp.getWidth();
			size.Height = bmp.getHeight();
		}
		return size;
	}
	
	@Override
	public Paint getPaint()
	{
		return this.paint;
	}

	@Override
	public void setPaint(Paint paint)
	{
		this.paint = paint;
	}

	public ICanvasDrawing getDrawFunc()
	{
		return this.mDrawingFunc;
	}

	public void setDrawFunc(ICanvasDrawing cd)
	{
		this.mDrawingFunc = cd;
	}
	
	//set methods
	//public void setFPS(int iFPS)
	//{
		//this.mFPS = iFPS;
	//}
	
	protected int getCurrentFrame()
	{
		return this.mResourceIndex;
	}
	
	public void setSelected(boolean isSelected)
	{
		this.mIsSelected = isSelected;
	}
	public boolean isSelected()
	{
		return this.mIsSelected;
	}

	@Override
	//get the bitmap through index, and paint the bitmap
	public void paint(Canvas canvas)
	{
		canvas.save();
		if (mDrawingFunc != null)
		{
			mDrawingFunc.paint(canvas, this);
		}
		canvas.restore();
	}

	private ICanvasDrawing mDrawingFunc = new ICanvasDrawing()
	{
		@Override
		public boolean paint(Canvas canvas, ISprite sprite)
		{
			if (!(sprite instanceof SpriteBasic)) return false;
			
//		       float[] colorTransform = {
//		                0, 1f, 0, 0, 0, 
//		                0, 0, 1f, 0, 0,
//		                0, 0, 0, 1f, 0, 
//		                0, 0, 0, 1f, 0};
//		        ColorMatrix colorMatrix = new ColorMatrix();
//		        colorMatrix.setSaturation(0.5f); //Remove Colour 
//		        colorMatrix.set(colorTransform); //Apply the Red
//		        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
			SpriteBasic sb = (SpriteBasic)sprite;
			

			//sb.getPaint().setColorFilter(new PorterDuffColorFilter(Color.argb(50, 255, 255, 255), Mode.SRC_OVER));
			ResourceManager manager = ResourceManager.getInstance();
			canvas.drawBitmap(manager.getBitmap(sb.getCurrentFrame()), 
					sb.mObjPos.x, sb.mObjPos.y, sb.paint);
			return true;
		}

		@Override
		public String getName()
		{
			return "basicdrawing";
		}
	};



}
